<?php
/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.cn / www.i-gray.cn
 *
 *	[ WARNING!!! ] This is a freeware!
 *	[ WARNING!!! ] But, You MUST get author's authorization before using it!
 *
 *	Name : MDKdatabase.lib.php / Development Code Evolve
 *	Created / Modify : 2009-9-24 / --
 */
	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	class	database
	{
		public		$error				=	"无法链接数据库";

		public		$MC					=	false;

		public		$queryTotal			=	0;					//	[CH]	统计MySql查询次数

		protected	$mysql_server_name	=	null;
		protected	$mysql_username		=	null;
		protected	$mysql_password		=	null;
		protected	$mysql_database		=	null;
		protected	$conn				=	null;
		protected	$query				=	null;
		protected	$assoc				=	null;
		protected	$row				=	null;
		protected	$rows				=	null;

		/*
		+----------------------------------+
		|	初始化数据库连接[构造函数]
		+----------------------------------+
		|	C / M : 2008-7-11 / --
		+----------------------------------+
		*/
		public	function	__construct( $MysqlServerName, $MysqlUserName, $MysqlPassword, $MysqlDatabase )
		{
			$this->mysql_server_name	=	$MysqlServerName;
			$this->mysql_username		=	$MysqlUserName;
			$this->mysql_password		=	$MysqlPassword;
			$this->mysql_database		=	$MysqlDatabase;

			if( ME_DB_PCONNECT )
				$this->conn	=	@mysql_pconnect( $this->mysql_server_name, $this->mysql_username, $this->mysql_password ) or die( $this->error . "<sup>[1]</sup>" );
			else
				$this->conn	=	@mysql_connect( $this->mysql_server_name, $this->mysql_username, $this->mysql_password ) or die( $this->error . "<sup>[1]</sup>" );

			if( $this->conn )
			{
				mysql_query( "set names 'utf8'" );
				mysql_query( "set sql_mode=''" );
				mysql_select_db( $this->mysql_database, $this->conn ) or die( $this->error . "<sup>[2]</sup>" );
			}

			$options = array(
				'servers'	=>	array( ME_MEMCACHED_SERVERS ),		//设置memcached 服务的地址、端口
				'debug'		=>	ME_MEMCACHED_DEBUG,					//是否打开 debug
				'compress_threshold'	=>	ME_MEMCACHED_COMPRESS,	//超过多少字节的数据时进行压缩
				'persistant'			=>	ME_MEMCACHED_PERSISTANT	//是否使用持久连接
			);

			$this->MC	=	&new MDKmemcached( $options );
		}

		/**
		 *	添加Memcached支持
		 *	[C/M]	-- / 2010-3-21
		 */
		public	function	query( $sql/*, $type = false 是否查询Memcache */ )
		{
			//	[CH]	多余的设计
			//	Fix : 2010-3-21
			//$this->queryTotal++;

			/*$keys	=	$this->MC && $type ? $sql : false;	//	[CH]	设置Memcached的key

			if( $type && $keys && $this->MC->get( $keys ) )
				return	$this->query	=	$this->MC->get( $keys );

			//$this->query	=	mysql_query( $sql );
			//if( $this->query )*/
				$this->queryTotal++;
			return	$this->query	=	mysql_query( $sql );
		}

		public	function	assoc( $result = false )
		{
			if( $result )
				$this->assoc	=	mysql_fetch_array( $result, MYSQL_ASSOC );
			else
				$this->assoc	=	mysql_fetch_array( $this->query, MYSQL_ASSOC );
			return	$this->assoc;
		}

		public	function	row( $result=false )
		{
			if( $result )
				$this->row	=	mysql_fetch_row( $result );
			else
				$this->row	=	mysql_fetch_row( $this->query );
			return	$this->row;
		}

		public	function	rows( $result=false )
		{
			if( $result )
				$this->rows	=	mysql_num_rows( $result );
			else
				$this->rows	=	mysql_num_rows( $this->query );
			return	$this->rows;
		}

		public	function	__destruct()
		{
			//$this->conn	=	null;
			if( $this->MC )
				$this->MC	=	null;
			mysql_close( $this->conn );
		}
	}

	$db	=	&new database( $MysqlServerName, $MysqlUserName, $MysqlPassword, $MysqlDatabase );
	unset( $MysqlServerName, $MysqlUserName, $MysqlPassword, $MysqlDatabase );
?>